package com.bankichi.nfcwrench;

import java.util.List;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import android.view.WindowManager;
import android.app.Activity;

public class Wrenches {

	public static void AutoBrightOn(Activity activity) {
		Settings.System.putInt(activity.getContentResolver(),
				Settings.System.SCREEN_BRIGHTNESS_MODE,
				Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
	}

	public static void AutoBrightOff(Activity activity) {

		WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
		lp.screenBrightness = (float) 0.05;
		// lp.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
		activity.getWindow().setAttributes(lp);

		Settings.System.putInt(activity.getContentResolver(),
				Settings.System.SCREEN_BRIGHTNESS_MODE,
				Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);

		Settings.System.putInt(activity.getContentResolver(),
				Settings.System.SCREEN_BRIGHTNESS, 1);

	}

	public static void AutoSyncOff() {
		ContentResolver.setMasterSyncAutomatically(false);
	}

	public static void AutoSyncOn() {
		ContentResolver.setMasterSyncAutomatically(true);
	}

	public static void WiFiOff(Context context) {
		((WifiManager) context.getSystemService(Context.WIFI_SERVICE))
				.setWifiEnabled(false);
	}

	public static void WiFiOn(Context context) {
		((WifiManager) context.getSystemService(Context.WIFI_SERVICE))
				.setWifiEnabled(true);
	}

	public static void startVPNSettings(Context context) {

		final Intent intent = new Intent("android.net.vpn.SETTINGS");
		context.startActivity(intent);

		// final String PACKAGE_PREFIX =
		// VpnManager.class.getPackage().getName() + ".";
		// final String ACTION_VPN_SETTINGS =
		// PACKAGE_PREFIX + "SETTINGS";
		// Intent intent = new Intent(ACTION_VPN_SETTINGS);
		// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		// mContext.startActivity(intent);
	}

	public static Boolean tryToConnectWifiBySSID(Context context, String ssid) {

		List<ScanResult> list;
		Boolean connectSucceed = false;

		WifiAdmin wfa = new WifiAdmin(context);

		String curSSID = wfa.getWifiInfo().getSSID();

		if (curSSID == null) {
			
			connectSucceed=false;

		} else {

			if (wfa.getWifiInfo().getSSID().equals(ssid)) {

				return true;

			} else {

				wfa.startScan();

				list = wfa.getWifiList();

				if (!(list == null)) {
					for (int i = 0; i < list.size(); i++) {
						if (list.get(i).SSID.equals(ssid)) {

							wfa.connectBySSID(ssid);
							connectSucceed = true;

						}
					}
				}
			}
		}

		return connectSucceed;
	}
}
